package com.xoa.service.cms;

import com.xoa.dao.cms.CmsSiteInfoMapper;
import com.xoa.dao.cms.SiteMouldMapper;
import com.xoa.model.cms.CmsSiteInfo;
import com.xoa.model.cms.CmsSiteInfoExample;
import com.xoa.model.cms.SiteMould;
import com.xoa.model.cms.SiteMouldExample;
import com.xoa.model.users.Users;
import com.xoa.service.cms.core.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by cyt on 2018/7/5.
 */
@Service
public class CmsSiteService {
    @Autowired
    CmsSiteInfoMapper cmsSiteInfoMapper;
    @Autowired
    SiteMouldMapper siteMouldMapper;

    public BaseWrapper addSiteInfo(CmsSiteInfo siteInfo, HttpServletRequest request){
        BaseWrapper baseWrapper=new BaseWrapper();
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        siteInfo.setCrUserid(user.getUid());
        siteInfo.setCrUsername(user.getUserId());
        siteInfo.setCrTime(new Date());
        siteInfo.setPubStatus(0);
        if(siteInfo.getSortNo()==null){
            siteInfo.setSortNo(9);
        }
        int i = cmsSiteInfoMapper.insertSelective(siteInfo);
        siteInfo.setPubPath("/cmsPub/portal?siteId="+siteInfo.getSid());
        cmsSiteInfoMapper.updateByPrimaryKeySelective(siteInfo);
        if (i > 0) {
            baseWrapper.setMsg("添加成功");
            baseWrapper.setFlag(true);
            baseWrapper.setFlag(true);
        }
        return baseWrapper;
    }
    /**
     * 获取站点信息
     * @param siteId
     * @return
     */
    public CmsSiteInfo getSiteInfo(int siteId){
        CmsSiteInfo siteInfo =  cmsSiteInfoMapper.selectByPrimaryKey(siteId);
        CmsSiteInfo infoModel = new CmsSiteInfo();
        BeanUtils.copyProperties(siteInfo, infoModel);
        infoModel.setCrTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(siteInfo.getCrTime()));
        //获取模板列表
        SiteMouldExample siteMouldExample=new SiteMouldExample();
        SiteMouldExample.Criteria criteria = siteMouldExample.createCriteria();
        criteria.andSiteIdEqualTo(siteId);
        List<SiteMould> siteMoulds = siteMouldMapper.selectByExample(siteMouldExample);
        infoModel.setSiteTemplates(siteMoulds);
        return infoModel;
    }
    public CmsSiteInfo getSiteInfoModelByIdentity(String id){
        CmsSiteInfoExample cmsSiteInfoExample=new CmsSiteInfoExample();
        CmsSiteInfoExample.Criteria criteria = cmsSiteInfoExample.createCriteria();
        criteria.andSiteIdentityEqualTo(id);
        List<CmsSiteInfo> cmsSiteInfos = cmsSiteInfoMapper.selectByExample(cmsSiteInfoExample);
        if(cmsSiteInfos==null){
            return null;
        }
        CmsSiteInfo infoModel = new CmsSiteInfo();
        BeanUtils.copyProperties(cmsSiteInfos.get(0), infoModel);
        infoModel.setCrTimeStr(FileUtils.format(cmsSiteInfos.get(0).getCrTime()));
        return infoModel;
    }
    public BaseWrapper updateSiteInfo(CmsSiteInfo siteInfo){
        BaseWrapper baseWrapper=new BaseWrapper();
        int i = cmsSiteInfoMapper.updateByPrimaryKeySelective(siteInfo);
        if (i > 0) {
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("更新成功");
            baseWrapper.setStatus(true);
        }
        return  baseWrapper;
    }
    public BaseWrapper delSiteInfo(Integer sid){
        BaseWrapper baseWrapper=new BaseWrapper();
        int i=cmsSiteInfoMapper.deleteByPrimaryKey(sid);
        if (i > 0) {
            baseWrapper.setFlag(true);
            baseWrapper.setStatus(true);
            baseWrapper.setMsg("删除成功");
        }
        return baseWrapper;
    }

    /**
    * @author 程叶同
    * @date 2018/7/10 16:02
    * @desc  获取站点列表
    */

    public BaseWrapper  getSiteList(){
        BaseWrapper baseWrapper=new BaseWrapper();
        CmsSiteInfoExample siteInfoExample=new CmsSiteInfoExample();
        CmsSiteInfoExample.Criteria criteria = siteInfoExample.createCriteria();
        siteInfoExample.setOrderByClause("SORT_NO ASC");
        List<CmsSiteInfo> cmsSiteInfos = cmsSiteInfoMapper.selectByExample(siteInfoExample);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        baseWrapper.setData(cmsSiteInfos);
        return  baseWrapper;
    }

    /**
     * @作者: 张航宁
     * @时间: 14:51
     * @说明: 修改发布状态
     */
    public BaseWrapper updatePubStatus(CmsSiteInfo siteInfo){
        BaseWrapper baseWrapper = new BaseWrapper();
        cmsSiteInfoMapper.updateByPrimaryKeySelective(siteInfo);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    public BaseWrapper getSiteInfoBySiteId(Integer siteId){
        BaseWrapper baseWrapper = new BaseWrapper();
        CmsSiteInfo cmsSiteInfo = cmsSiteInfoMapper.selectByPrimaryKey(siteId);
        baseWrapper.setData(cmsSiteInfo);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }
}
